<?php
class CompositionController extends AdminAppController{
    
    function index(){
        $this->loadModel('Admin.Composition');
        $compo = $this->Composition->get_compo();
        $list_compo = array();
        foreach ($compo as $com) {
            $temp = array();
            $temp['compo_id'] = $com['Composition']['compo_id'];
            $temp['title'] = $com['Composition']['title'];
            $temp['sort_order'] = $com['Composition']['sort_order'];
            $temp['status'] = $com['Composition']['status'];
            $temp['group'] = $com['Composition']['group'];
            $list_compo[] = $temp;
        }
        $this->set('list_compo', $list_compo);
    }
    
    function add(){
        $this->loadModel('Admin.Language');
        $this->loadModel('Admin.Composition');
        
        $list_language = $this->Language->get_language();
        $this->set('list_language', $list_language);
        if ($this->request->is('post')) {
            $compo_id = "";
            foreach($list_language as $language)
            {
                $language = $language['language'];
                $lang_code = $language['lang_code'];
                $data = array(
                        'title' => $this->request->data['Composition']['title_'.$lang_code],
                        'description' => $this->request->data['Composition']['description_'.$lang_code],
                        'status' => $_POST["status"],
                        'lang_code' => $lang_code,
                        'group' => $this->request->data['Composition']['group'],
                        'sort_order' => $this->request->data['Composition']['sort_order'],
                        );
                if($compo_id != "")
                    $data['compo_id'] = $compo_id;
                $compo_id = $this->Composition->add($data);
            }
            $this->Session->setFlash(
                                'Thêm mới thành phần thành công',
                                'default',
                                array('class' => 'alert alert-success')
                            );
            
            $this->redirect(array("controller" => "composition", 
                                  "action" => "index"));
        }
    }
    
    function edit($compo_id = ""){
        if($compo_id == ""){
            $this->redirect(array("controller" => "composition", 
                                  "action" => "index")
                            );
            exit;
        }
        
        $this->set('compo_id', $compo_id);
        $this->loadModel('Admin.Language');
        $this->loadModel('Admin.Composition');


        $compo_info = $this->Composition->get_compo_by_id($compo_id, 'all');
        $this->set('compo_info', $compo_info);
        
        $list_language = $this->Language->get_language();
        $this->set('list_language', $list_language);
        
        
        if ($this->request->is('post')) {
            foreach($list_language as $language)
            {
                $language = $language['language'];
                $lang_code = $language['lang_code'];
                $data = array(
                        'title' => "'".$this->request->data['Composition']['title_'.$lang_code]."'",
                        'description' => "'".$this->request->data['Composition']['description_'.$lang_code]."'",
                        'group' => "'".$this->request->data['Composition']['group']."'",
                        'sort_order' => "'".$this->request->data['Composition']['sort_order']."'",
                        );
                
                $this->Composition->update($compo_id, $lang_code, $data);
            }
            $this->Session->setFlash(
                                'Sửa thành phần thành công',
                                'default',
                                array('class' => 'alert alert-success')
                            );
            $this->redirect(array("controller" => "composition", 
                                  "action" => "index"));
        }
    }
}
